package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.commen.R;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.DishFlavor;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.service.ICategoryService;
import com.itheima.reggie.service.IDishFlavorService;
import com.itheima.reggie.service.IDishService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/dish")
public class DishController {
    @Autowired
    private IDishService service;
    @Autowired
    private ICategoryService categoryService;
    @Autowired
    private IDishFlavorService flavorService;


    @PostMapping
    public R save(@RequestBody DishDto dishDto) {
        service.saveWithFlavor(dishDto);
        return R.success("添加成功");
    }

    @GetMapping("/page")
    public R page(Integer page, Integer pageSize, String name) {
        IPage<Dish> page1 = new Page<>(page, pageSize);
        IPage<DishDto> dtoshPage = new Page<>();
        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
        qw.orderByDesc(Dish::getUpdateTime);
        qw.like(StringUtils.isNotEmpty(name), Dish::getName, name);
        service.page(page1, qw);
        BeanUtils.copyProperties(page1, dtoshPage, "records");
        List<DishDto> records = dtoshPage.getRecords();
        List<DishDto> list = records.stream().map((item) -> {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(item, dishDto);
            Long categoryId = item.getCategoryId();
            Category category = categoryService.getById(categoryId);
            if (category != null) {
                String categoryName = category.getName();
                dishDto.setCategoryName(categoryName);
            }
            return dishDto;
        }).collect(Collectors.toList());
        dtoshPage.setRecords(list);
        return R.success(page1);
    }

    //  根据id查询菜品数据，用于回显
    @GetMapping("/{id}")
    public R getById(@PathVariable Long id) {
        Dish dish = service.getById(id);
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish, dishDto);

        LambdaQueryWrapper<DishFlavor> qw = new LambdaQueryWrapper<>();
        qw.eq(DishFlavor::getDishId, dish.getId());

        List<DishFlavor> list = flavorService.list(qw);

        dishDto.setFlavors(list);
        return R.success(dishDto);
    }

    @PutMapping
    public R update(@RequestBody DishDto dishDto) {
        service.updateById(dishDto);
        Long id = dishDto.getId();
        LambdaQueryWrapper<DishFlavor> qw = new LambdaQueryWrapper<>();
        qw.eq(DishFlavor::getDishId, id);
        flavorService.remove(qw);
        List<DishFlavor> flavors = dishDto.getFlavors();
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dishDto.getId());
        }
        flavorService.saveBatch(flavors);
        return R.success("修改菜品成功");
    }

    @DeleteMapping
    public R delete(@RequestParam List<Long> ids) {
//        service.removeByIds(ids);
        LambdaQueryWrapper<DishFlavor> qw=new LambdaQueryWrapper<>();
        qw.in(DishFlavor::getDishId,ids);
        flavorService.remove(qw);
        return R.success("删除成功");
    }

    @PostMapping("/status/{status}")
    public R updateStatus(@PathVariable Integer status,
                          @RequestParam List<Long> ids){
    /*    LambdaQueryWrapper<Dish> qw=new LambdaQueryWrapper<>();
        qw.in(Dish::getId,ids);
        List<Dish> list = service.list(qw);
        for (Dish dish : list) {
            Integer status1 = dish.getStatus();
            if (status1==1){
                dish.setStatus(0);

            }else if(status1==0){
                dish.setStatus(1);
            };
            service.updateById(dish);
        }*/
        LambdaUpdateWrapper<Dish> qw=new LambdaUpdateWrapper<>();
        qw.set(Dish::getStatus,status);
        qw.in(Dish::getId,ids);
        service.update(qw);
        return R.success("操作成功");
    }
    @GetMapping("/list")
    public R list(@RequestParam Long categoryId){

        LambdaQueryWrapper<Dish> qw=new LambdaQueryWrapper<>();
        qw.eq(Dish::getCategoryId,categoryId);
        List<Dish> dishList = service.list(qw);
        ArrayList<DishDto> dishDtos = new ArrayList<>();

        for (Dish dish : dishList) {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(dish,dishDto);
            LambdaQueryWrapper<DishFlavor> qw1=new LambdaQueryWrapper<>();
            qw1.eq(DishFlavor::getDishId,dish.getId());
            List<DishFlavor> list = flavorService.list(qw1);
            dishDto.setFlavors(list);
            dishDtos.add(dishDto);
        }
        return R.success(dishDtos);
    }



}

